/**
 * This file is part of decent-cpp.
 *
 * decent-cpp is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * decent-cpp is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with decent-cpp.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <array>
#include <nonstd/optional.hpp>
#include <string>
#include <cstring>
#include "dcstdint.h"

namespace dc {
/// UUID type.
typedef std::array<u8_t, 16> uuid;

/// Convert UUID to string
std::string uuid_to_string(uuid id);

/// Convert string to UUID
nonstd::optional<uuid> string_to_uuid(std::string string);

/// Fill the given uuid with random bytes
uuid random_uuid();

/// Generate a hash for the given password. May throw if libsodium errors.
std::string pwd_hash(std::string pwd);

/// Verify that the given password matches the given hash
bool pwd_verify(std::string pwd, std::string hash);
}  // namespace dc

namespace std {
/// UUID hasher, for maps
template <>
struct hash<dc::uuid> {
  std::size_t operator()(dc::uuid const& u) const {
    dc::u64_t long1;
    std::memcpy(&long1, u.data(), 8);
    dc::u64_t long2;
    std::memcpy(&long2, u.data() + 8, 8);
    return long1 + long2;
  }
};
}  // namespace std
